//
//  AddressCell.m
//  jang
//
//  Created by lee han on 23/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "AddressCell.h"
#import "CommonView.h"
#import "CRUserData.h"

@implementation AddressCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    _mContainView.layer.cornerRadius = 8;
    _mContainView.layer.masksToBounds = YES;
    _mContainView.layer.borderWidth = 1;
    _mContainView.layer.borderColor = [[UIColor groupTableViewBackgroundColor] CGColor];
}

-(void)initWith:(NSDictionary*)data type:(NSInteger)type delegate:(id<AddressCellDelegate>) delegate{
    mDelegate = delegate;
    mAddressId = [[data objectForKey:@"id"] integerValue];
    mType = type;
    _mTextName.text = [data objectForKey:@"name"];
    _mTextPhone.text = [data objectForKey:@"phone"];
    _mTextAddress.text = [data objectForKey:@"address"];
    
    if(type == 0){
        [self setCellSelected:[[data objectForKey:@"selected"] integerValue] == 1];
    }
}

-(void)setCellSelected:(BOOL)selected{
    if(mType == 0){
        if (selected) {
            _mButtonSelect.backgroundColor = [UIColor redColor];
            [_mButtonSelect setTitle:@"默认" forState:UIControlStateNormal];
        }
        else {
            _mButtonSelect.backgroundColor = [UIColor darkGrayColor];
            [_mButtonSelect setTitle:@"可用" forState:UIControlStateNormal];
        }
    }
    else {
        if (selected) {
            _mButtonSelect.backgroundColor = [UIColor redColor];
            [_mButtonSelect setTitle:@"已选" forState:UIControlStateNormal];
        }
        else {
            _mButtonSelect.backgroundColor = [UIColor darkGrayColor];
            [_mButtonSelect setTitle:@"可用" forState:UIControlStateNormal];
        }
    }
    mSelected = selected;
}

- (IBAction)onClickSelect:(id)sender {
    if(mSelected)
        return;
    
    if(mType == 0){
        NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld", (long)mAddressId], @"aid", nil];
        
        [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_AddressSelect];
    }
    else{
        [mDelegate onAddressCellSelected:mAddressId];
    }
}

- (IBAction)onClickDelete:(id)sender {
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld", (long)mAddressId], @"aid", nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_DelAddress];
}

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if([[response objectForKey:@"status"] integerValue] == 1){
        if (type == NetworkFunctionType_DelAddress){
            [[CRUserData sharedInstance] delAddressData:[[response objectForKey:@"aid"] integerValue]];
            if([response objectForKey:@"selectid"]){
                [[CRUserData sharedInstance] setAddressSelect:[[response objectForKey:@"selectid"] integerValue] old:0];
            }
            [mDelegate onAddressCellChanged];
        }
        else if(type == NetworkFunctionType_AddressSelect){
            [[CRUserData sharedInstance] setAddressSelect:[[response objectForKey:@"aid"] integerValue] old:[[response objectForKey:@"old"] integerValue]];
            
            [mDelegate onAddressCellChanged];
        }
    }
    else{
        [CommonView addAlertView:nil Text:@"操作错误"];
    }
}
//end networkDelegate
@end
